package com.xjj.util.utils;

import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * @author sjy
 * @create 2022-10-20 11:08
 */
public class RandomUtil {
    private final static String IMG = "img-";//图片前缀

    public static String getImgRandom() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSSS");
        String id = sdf.format(System.currentTimeMillis());
        Random random = new Random();
        int num = random.nextInt(10000);
        return IMG + id + num;
    }

    /**
     * 产生随机4位验证码
     *
     * @return
     */
    public static String getVerrityCode() {
        Random r = new Random();
        String code = "";
        String str = "0123456789";//列出所有的字母数字
        for (int i = 0; i < 6; i++)//循环6次，输出六个数
        {
            int a = r.nextInt(10);//从0-9中随机一个数，作为字符串的索引

            code += str.substring(a, a + 1);//从字符串中利用索引找到输出它
        }
        return code;
    }
}
